from utile.GetKeyWord import GetKeyWord
from utile.sendmethod import SendMenthod


class Memberinterface:
    def __init__(self, url):
        self.url = url

    # 注册
    def register(self, params):
        method = 'post'
        url = self.url + '/sso/register'
        return SendMenthod.send_method(method=method, url=url, data=params)

    # 获取手机验证码
    def get_auth_code(self, params):
        method = 'get'
        url = self.url + '/sso/getAuthCode'
        return SendMenthod.send_method(method=method, url=url, params=params)

    # 提取验证码
    def get_verity_code(self, params):
        resource = self.get_auth_code(params)
        return GetKeyWord.get_keyword(resource, 'data')

    # 登录
    def login(self, params):
        method = 'post'
        url = self.url + '/sso/login'
        return SendMenthod.send_method(method=method, url=url, data=params)

    # 获取token
    def get_token(self, params):
        resource = self.login(params)
        token = GetKeyWord.get_keyword(resource, 'token')
        return {'Authorization': f'Bearer {token}'}

    # 获取会员信息
    def get_member_info(self, headers):
        method = 'get'
        url = self.url + '/sso/info'
        return SendMenthod.send_method(method=method, url=url,headers=headers)

    # 会员修改密码
    def update_password(self, headers):
        method = 'post'
        url = self.url + '/sso/updatePassword'
        return SendMenthod.send_method(method=method, url=url,headers=headers)

    # 刷新token
    def refreshToken(self,headers):
        method = 'get'
        url = self.url + '/sso/refreshToken'
        return SendMenthod.send_method(method=method, url=url,headers=headers)


if __name__ == '__main__':
    url = 'http://47.108.206.100:8085'
    mb = Memberinterface(url)
    telephone = 18871463422
    # params = {'telephone': telephone}
    # payload = {
    #     'username': 'ymYM',
    #     'password': '654321',
    #     'telephone': telephone,
    #     'authCode': mb.get_verity_code(params)
    # }
    # print(mb.register(payload))
    params = {
        'username': 'ymYM',
        'password': '654321'
    }
    # print(mb.login(params))
    # print(mb.get_token(params))
    # updata_params = {
    #     'username': 'ymYM',
    #     'password': '654321',
    #     'authcode': mb.get_verity_code(params={"telephone": telephone})
    # }
    # print(mb.update_password(updata_params))
    # print('token')
    # print(mb.refreshToken(headers=token))
